﻿using System;
using System.ComponentModel;
using System.Windows;

namespace Coding4Fun.MissionControl.WP.Misc
{
    public abstract class BindableBase : INotifyPropertyChanged
    {
        public BindableBase()
        {
            // So that the deserializer won't throw a SecurityException
        }

        public bool SetProperty<T>(ref T storage, T value, string propertyName = null)
        {
            if (object.Equals(storage, value))
                return false;

            storage = value;
            NotifyPropertyChanged(propertyName);

            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(info));
                    });
            }
        }
    }
}
